/**
 * 
 */
package com.sc.softmgr.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.model.SoftmgrCategory;
import com.acc.softmgr.model.SoftmgrCategoryExample;
import com.acc.softmgr.model.SysUser;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sc.softmgr.business.service.ISoftmgrCategoryService;
import com.sc.softmgr.common.constant.DeleteStatus;
import com.sc.softmgr.common.constant.enums.RestStatus;
import com.sc.softmgr.common.utils.date.DateBase;
import com.sc.softmgr.common.web.DataGridObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("softCategory")
public class SoftmgrCategoryController {

	@Autowired
	private ISoftmgrCategoryService softmgrCategoryService;

	@RequestMapping(value = "toSoftmgrCategory", method = RequestMethod.GET)
	public String toSoftmgrCategory() {
		return "softCategory/softCategory";
	}

	/**
	 * 查询列表页
	 * 
	 * @param project_name
	 * @param project_english
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSoftmgrCategory", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<SoftmgrCategory> searchSoftmgrCategory(
			@RequestParam(value = "searchCategoryId", required = false) Integer searchCategoryId,
			@RequestParam Integer page, @RequestParam Integer rows) throws Exception {
		DataGridObject<SoftmgrCategory> responseObject = new DataGridObject<SoftmgrCategory>();
		SoftmgrCategoryExample example = new SoftmgrCategoryExample();
		System.out.println(searchCategoryId);
		if (searchCategoryId != null) {
			example.getCriteria().andIdEqualTo(searchCategoryId);
		}
		if (page == null || page == 0) {
			page = 1;
		}

		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		List<SoftmgrCategory> SoftmgrCategorys = softmgrCategoryService.selectByExample(example);
		for (SoftmgrCategory SoftmgrCategory : SoftmgrCategorys) {
			SoftmgrCategory.setCreateTimestring(
					DateBase.formatDate(SoftmgrCategory.getCreatetime(), DateBase.DATE_PATTERN_DATETIME));
		}
		responseObject.setTotal(page * rows);
		responseObject.setRows(SoftmgrCategorys);
		return responseObject;
	}

	/**
	 * 新增项目
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSoftmgrCategory", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject addSoftmgrCategory(SoftmgrCategory softmgrCategory, HttpServletRequest request) throws Exception {
		JSONObject json = new JSONObject();
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");
		if (StringUtils.isEmpty(softmgrCategory.getCategory_name())) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		} else {
			// ----------判断唯一性------------
			SoftmgrCategoryExample example = new SoftmgrCategoryExample();
			example.getCriteria().andCategory_nameEqualTo(softmgrCategory.getCategory_name());
			example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
			List<SoftmgrCategory> SoftmgrCategorys = softmgrCategoryService.selectByExample(example);
			if (SoftmgrCategorys != null && SoftmgrCategorys.size() > 0) {
				json.put("status", RestStatus.NOT_UNIQUE.code);
				json.put("message", RestStatus.NOT_UNIQUE.message);
				return json;
			} else {
				SoftmgrCategory sc = new SoftmgrCategory();
				sc.setCreatetime(new Date());
				sc.setCreateby(currentUser.getUsername());
				sc.setCategory_name(softmgrCategory.getCategory_name());
				sc.setDeleted(DeleteStatus.FALSE);
				softmgrCategoryService.insert(sc);
				json.put("status", RestStatus.SUCCESS.code);
				json.put("message", RestStatus.SUCCESS.message);
				return json;
			}

		}

	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSoftmgrCategory", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject editSoftmgrCategory(SoftmgrCategory softmgrCategory, HttpServletRequest request)
			throws Exception {
		JSONObject json = new JSONObject();
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");
		if (StringUtils.isEmpty(softmgrCategory.getCategory_name()) || softmgrCategory.getId() == null
				|| softmgrCategory.getId() == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		} else {
			// ----------判断唯一性------------
			SoftmgrCategory SoftmgrCategory = softmgrCategoryService.selectByPrimaryKey(softmgrCategory.getId());
			boolean exitSoftmgrCategory = exitSoftmgrCategory(softmgrCategory.getCategory_name());
			if (exitSoftmgrCategory) {
				json.put("status", RestStatus.NOT_UNIQUE.code);
				json.put("message", RestStatus.NOT_UNIQUE.message);
				return json;
			} else {
				SoftmgrCategory.setCategory_name(softmgrCategory.getCategory_name());
				SoftmgrCategory.setUpdateby(currentUser.getUsername());
				SoftmgrCategory.setUpdatetime(new Date());
				softmgrCategoryService.updateByPrimaryKey(SoftmgrCategory);
				json.put("status", RestStatus.SUCCESS.code);
				json.put("message", RestStatus.SUCCESS.message);
				return json;
			}
		}
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchSoftmgrCategory", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject searchUser(@PathVariable Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null || id == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SoftmgrCategory softmgrCategory = softmgrCategoryService.selectByPrimaryKey(id);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		json.put("data", softmgrCategory);
		return json;
	}

	/**
	 * 删除项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSoftmgrCategory", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject removeSoftmgrCategory(@RequestParam Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null || id == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SoftmgrCategory SoftmgrCategory = softmgrCategoryService.selectByPrimaryKey(id);
		SoftmgrCategory.setDeleted(DeleteStatus.TRUE);
		softmgrCategoryService.updateByPrimaryKey(SoftmgrCategory);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		return json;
	}

	/**
	 * 获取所有Category项目
	 * 
	 * @return
	 */
	@RequestMapping(value = "getSoftmgrCategorys", method = RequestMethod.GET)
	@ResponseBody
	public String getSoftmgrCategorys() throws Exception {

		SoftmgrCategoryExample example = new SoftmgrCategoryExample();
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);

		List<SoftmgrCategory> list = softmgrCategoryService.selectByExample(example);
		JSONArray jsonArray = new JSONArray();
		for (SoftmgrCategory category : list) {
			JSONObject json = new JSONObject();
			json.put("id", category.getId());
			json.put("text", category.getCategory_name());
			jsonArray.add(json);
		}

		return jsonArray.toJSONString();
	}

	private boolean exitSoftmgrCategory(String category_name) {
		SoftmgrCategoryExample example = new SoftmgrCategoryExample();
		example.getCriteria().andCategory_nameEqualTo(category_name);
		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		List<SoftmgrCategory> SoftmgrCategorys = softmgrCategoryService.selectByExample(example);
		if (SoftmgrCategorys != null && SoftmgrCategorys.size() > 0) {
			return true;
		} else {
			return false;
		}
	}
}
